<template>
  <div class="login">
    <van-form @submit="onSubmit">
      <van-field
        v-model="state.username"
        name="email"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="state.password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button round block type="primary" native-type="submit">
          提交
        </van-button>
      </div>
    </van-form>
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive } from "vue";
import { get, post } from "@/api/http";
import { Toast } from "vant";
import { useRouter } from "vue-router";
import { useStore } from '../../store';
export default defineComponent({
  name: "Login",
  components: {},
  setup() {
    const router = useRouter();
    const store = useStore()
    const state = reactive({
      username: "",
      password: "",
    });
    const onSubmit = async (values: any) => {
      const res: any = await post("/login", values);
      if (res.status != 200) {
        Toast.fail(res.msg);
        return;
      }
      Toast.success(res.msg);
      store.state.userInfo = res.data;
      window.sessionStorage.setItem("token", res.data.Bearer);
      window.sessionStorage.setItem("leaveId", res.data.id);
      router.push("/home");
    };
    return {
      state,
      onSubmit,
    };
  },
});
</script>
